package com.nf.health.app.activity.home.adapter;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.login.activity.LoginActivity;
import com.nf.health.app.activity.home.CharacteristicDepartmentActivity;
import com.nf.health.app.activity.home.HospitalIntroductionActivity;
import com.nf.health.app.activity.home.MengYiCultrueActivity;
import com.nf.health.app.activity.home.NewWebViewActivity;
import com.nf.health.app.activity.home.SatisfactionSurveyQuestionActivity;
import com.nf.health.app.activity.home.SearchNewDoctorActivity;
import com.nf.health.app.activity.home.bean.MyBinnerHomeModel;
import com.nf.health.app.activity.home.bean.MyHomeHospitalConsultationModel;
import com.nf.health.app.customview.CircleProgress;
import com.nf.health.app.customview.MyListView;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.models.HealthStateInfo;
import com.nf.health.app.models.HospatalStateBean;
import com.nf.health.app.models.WebModel;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.widget.InfiniteViewPager;
import com.nf.health.app.widget.indicator.CirclePageIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Jason on 2016/3/25.
 */
public class NewHomeAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    /**
     * 广告位
     */
    private static final int TYPE_ADS = 0;
    /**
     * 功能入口:蒙医院+名医专家 + 院内咨询
     */
    private static final int TYPE_FUNCION_ENTRY = 1;
    /**
     * 院内咨讯列表
     */
    private static final int TYPE_HOSTIPAL_ASK = 2;
    /**
     * 医院图片 挂号状态
     */
    private static final int TYPE_HOSTIPAL_STATE = 3;


    private List<MyBinnerHomeModel> binnerList;
    private List<DoctorVo> myDoctor;
    private HospatalStateBean hospatalStateBean;
    private List<MyHomeHospitalConsultationModel> hospitalInformationCinlusionModeList;
    private NewAdsAdapter adsPagerAdapter;
    LayoutInflater inflater;
    private Activity act;
    private String docotor_hid;
    private HealthStateInfo healthStateInfo;

    public NewHomeAdapter(Activity act, HealthStateInfo healthStateInfo,String docotor_hid) {
        this.act = act;
        this.docotor_hid = docotor_hid;
        this.healthStateInfo = healthStateInfo;
        inflater = LayoutInflater.from(act);
    }

    public void setHealthStateInfo(HealthStateInfo healthStateInfo) {
        this.healthStateInfo = healthStateInfo;
    }

    public void setMyNotifyChanged(HealthStateInfo healthStateInfo) {
        this.healthStateInfo = healthStateInfo;
        //notifyDataSetChanged();;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ADS) {   //广告位
            View viewAds = inflater.inflate(R.layout.item_home_ads, null);//1
            return new AdsViewHolder(viewAds);
        } else if (viewType == TYPE_FUNCION_ENTRY) {  // 功能入口:蒙医院+名医专家 + 院内咨询
            View viewFunEntry = inflater.inflate(R.layout.item_home_fun_entry_hostipal, null);//item_home_fun_entry
            return new FunEntryViewHolder(viewFunEntry);
        } else if (viewType == TYPE_HOSTIPAL_ASK) {//  院内咨询
            View viewHealthScheme = inflater.inflate(R.layout.item_home_hospital_navigation, null);
            return new HealthNewsHeaderViewHolder(viewHealthScheme);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AdsViewHolder) {  //广告位
            AdsViewHolder  adsHolder = ((AdsViewHolder) holder);
            if(binnerList == null){
                binnerList = new ArrayList<>();
            }
            adsPagerAdapter =   new NewAdsAdapter(act,binnerList);
            adsHolder.viewpager.setAdapter(adsPagerAdapter);
            adsHolder.viewpager.startAutoScroll();
            adsHolder.indicator.setViewPager(adsHolder.viewpager, 0);
            adsHolder.viewpager.setClickable(false);
        } else if (holder instanceof HealthNewsHeaderViewHolder) { // 院内咨询
            HealthNewsHeaderViewHolder  healthNewsHeaderViewHolder = (NewHomeAdapter.HealthNewsHeaderViewHolder) holder;
            if(hospitalInformationCinlusionModeList != null){
//                if(hospitalInformationCinlusionModeList.size() > 0){
//                    MyHomeHospitalConsultationModel myHomeHospitalConsultationModel = hospitalInformationCinlusionModeList.get(0);
//                    //显示第一张图片
//                    GlideUtil.loadNetImgC(act, healthNewsHeaderViewHolder.iv_hotipal_navigation, myHomeHospitalConsultationModel.getHimg(), R.drawable.default_image);
//                }else{
//                    GlideUtil.loadLocalImg(act, healthNewsHeaderViewHolder.iv_hotipal_navigation, R.drawable.default_image);
//                }
               /* List<MyHomeHospitalConsultationModel> list = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    MyHomeHospitalConsultationModel myHomeHospitalConsultationModel = new MyHomeHospitalConsultationModel();
                    myHomeHospitalConsultationModel.setImgUrl("http://i2.muimg.com/583828/1801df3f8e639f2d.jpg");
                    myHomeHospitalConsultationModel.setContentMsg("账单该换了");
                    myHomeHospitalConsultationModel.setContentMsg("海词词典,最权威的学习词典,专业出版咨询内容的英文,咨询内容翻译,咨询内容英语怎么说等详细讲解。海词词典:学习变容易,记忆很深刻。");
                    list.add(myHomeHospitalConsultationModel);
                }
                if(list != null && list.size() > 6){
                    list = list.subList(0,6);
                }*/
                HostipalConsulateAdapter hostipalConsulateAdapter = new HostipalConsulateAdapter(act, hospitalInformationCinlusionModeList);
                healthNewsHeaderViewHolder.lv_hotipal_navigation.setAdapter(hostipalConsulateAdapter);
            }
        } else if (holder instanceof FunEntryViewHolder) { // 功能
            FunEntryViewHolder funEntryViewHolder = (FunEntryViewHolder) holder;
            if(hospatalStateBean == null){
                funEntryViewHolder.tv_service_pro.setEnabled(false);
                funEntryViewHolder.tv_service_hospital_navigation.setEnabled(false);
                funEntryViewHolder.tv_service_survey.setEnabled(false);
                return ;
            }
             String registrtype =  hospatalStateBean.getRegistrtype();//挂号状态  1 显示 2不显示
             String flag2 =  hospatalStateBean.getFlag2(); //院内导航    1 显示 2不显示
             String flag3 =  hospatalStateBean.getFlag3(); //满意度调查 1显示 2不显示


            ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_hospital_detail, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_home_intorduce);
            ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_hospital_feature, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_tske);
            ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_my_doctor, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_my_docotr);

            if(TextUtils.isEmpty(hospatalStateBean.getHospitalimg())){
                funEntryViewHolder.iv_hospital_culture.setVisibility(View.GONE);
            }else{
                funEntryViewHolder.iv_hospital_culture.setVisibility(View.VISIBLE);
                ImageLoaderUtils.getinstance(act).getImage(funEntryViewHolder.iv_hospital_culture,hospatalStateBean.getHospitalimg(),R.drawable.default_image);
            }

            //  ViewUtil.setTextImgBackground2(tv_service_pro, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yygh);
          //  ViewUtil.setTextImgBackground2(tv_service_hospital_navigation, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yndh);
          //  ViewUtil.setTextImgBackground2(tv_service_survey, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_survey_satify);
              if(!TextUtils.isEmpty(registrtype)){
                if(registrtype.equals("1")){//
                    funEntryViewHolder.tv_service_pro.setEnabled(true);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_pro, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yygh);
                }else{
                    funEntryViewHolder.tv_service_pro.setEnabled(false);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_pro, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yygh_disable);
                }
              }
            if(!TextUtils.isEmpty(flag2)){
                if(flag2.equals("1")){//
                    funEntryViewHolder.tv_service_hospital_navigation.setEnabled(true);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_hospital_navigation, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yndh);
                }else{
                    funEntryViewHolder.tv_service_hospital_navigation.setEnabled(false);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_hospital_navigation, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_yndh_disable);
                }
            }
            if(!TextUtils.isEmpty(flag3)){
                if(flag3.equals("1")){
                    funEntryViewHolder.tv_service_survey.setEnabled(true);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_survey, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_survey_satify);
                }else{
                    funEntryViewHolder.tv_service_survey.setEnabled(false);
                    ViewUtil.setTextImgBackground2(funEntryViewHolder.tv_service_survey, act, ViewUtil.TextMode.TOP, 0, R.drawable.iv_survey_satify_disable);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        /*HEADER_ITEM_COUNT = 1 + 1 + 1 + 1 + 1;//健康资讯上面item的数量(健康资讯有多个)
        if (healthNewsBean != null && healthNewsBean.getList() != null) {
            return HEADER_ITEM_COUNT + healthNewsBean.getList().size();
        }*/
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_ADS;
        } else if (position == 1) {
            return TYPE_FUNCION_ENTRY;
        } else if (position == 2) {
            return TYPE_HOSTIPAL_ASK;
        }
        return TYPE_HOSTIPAL_ASK;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_circle_progress:
                //  ActivityUtils.showActivity(act, HealthStateActivity.class);
                break;
            case R.id.rl_health_scheme_header:
                Toast.makeText(act, "添加新方案", Toast.LENGTH_SHORT).show();
                break;
            case R.id.rl_health_news_header:
                Toast.makeText(act, "更多资讯", Toast.LENGTH_SHORT).show();
                break;

        }
    }


    public void setAutoScroll(boolean isAutoScroll) {
     /*  if (adsHolder != null) {
            if (isAutoScroll) {
                adsHolder.viewpager.startAutoScroll();
            } else {
                adsHolder.viewpager.stopAutoScroll();
            }
        }*/
    }
    /**
     * 首页binner
     * 复制并刷新
     *
     * @param
     */
    public void setBinnerListContentMsg(List<MyBinnerHomeModel> binnerList) {
        this.binnerList = binnerList;
        getItemViewType(TYPE_ADS);
        notifyItemChanged(TYPE_ADS);
    }

    /**
     *名医专家列表
     *
     * @param myDoctor
     */
    public void setDoctorAskConsulision( List<DoctorVo> myDoctor ) {
        this.myDoctor = myDoctor;
        getItemViewType(TYPE_FUNCION_ENTRY);
        notifyItemChanged(TYPE_FUNCION_ENTRY);
    }
    /**
     *医院图片 挂号状态
     */
    public void setHospitalConsulision(HospatalStateBean hospatalStateBean) {
        this.hospatalStateBean = hospatalStateBean;
        getItemViewType(TYPE_FUNCION_ENTRY);
        notifyItemChanged(TYPE_FUNCION_ENTRY);
    }

    /**
     *  院内资讯列表
     *
     * @param hospitalInformationCinlusionModeList
     */
    public void sethospitalInformationCinlusionModeList(List<MyHomeHospitalConsultationModel> hospitalInformationCinlusionModeList) {
        this.hospitalInformationCinlusionModeList = hospitalInformationCinlusionModeList;
        getItemViewType(TYPE_HOSTIPAL_ASK);
        notifyItemChanged(TYPE_HOSTIPAL_ASK);
    }

    /**
     * 广告位
     */
    class AdsViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.viewpager)
        InfiniteViewPager viewpager;
        @Bind(R.id.indicator)
        CirclePageIndicator indicator;
        @Bind(R.id.rl_viewpager)
        RelativeLayout rl_viewpager;

        public AdsViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            ViewGroup.LayoutParams params = rl_viewpager.getLayoutParams();
            params.width = DensityUtil.getScreenIntWidth(act);
            params.height = DensityUtil.getScreenIntWidth(act) * 300 / 750;
            rl_viewpager.setLayoutParams(params);

        }
    }

    /**
     * 功能入口
     */
    class FunEntryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.iv_hospital_culture)
        ImageView iv_hospital_culture;

        @Bind(R.id.tv_hospital_detail)
        TextView tv_hospital_detail;
        @Bind(R.id.tv_hospital_feature)
        TextView tv_hospital_feature;


        @Bind(R.id.tv_service_pro)
        TextView tv_service_pro;

        @Bind(R.id.tv_service_my_doctor)
        TextView tv_service_my_doctor;

        @Bind(R.id.tv_service_hospital_navigation)
        TextView tv_service_hospital_navigation;
        @Bind(R.id.tv_service_survey)
        TextView tv_service_survey;



        public FunEntryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            iv_hospital_culture = (ImageView) itemView.findViewById(R.id.iv_hospital_culture);
            tv_hospital_detail.setOnClickListener(this);
            iv_hospital_culture.setOnClickListener(this);
            tv_hospital_feature.setOnClickListener(this);
            tv_service_pro.setOnClickListener(this);
            tv_service_my_doctor.setOnClickListener(this);
            tv_service_hospital_navigation.setOnClickListener(this);
            tv_service_survey.setOnClickListener(this);

            ViewGroup.LayoutParams params  = iv_hospital_culture.getLayoutParams();
            params.width = DensityUtil.getScreenIntWidth(act); // 750 * 172
            params.height = DensityUtil.getScreenIntWidth(act) * 172 / 750;
            iv_hospital_culture.setLayoutParams(params);
        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_hospital_culture://蒙医文化
                    Bundle bundle3 = new Bundle();
                    bundle3.putString(MengYiCultrueActivity.HID,docotor_hid);
                    ActivityUtils.showActivity(act, MengYiCultrueActivity.class,bundle3);
                    break;
                case R.id.tv_hospital_detail://医院介绍
                    Bundle bundle4 = new Bundle();
                    bundle4.putString(HospitalIntroductionActivity.HID,docotor_hid);
                    ActivityUtils.showActivity(act, HospitalIntroductionActivity.class,bundle4);
                    break;
                case R.id.tv_hospital_feature://特色科室
                    Bundle bundle5 = new Bundle();
                    bundle5.putString(CharacteristicDepartmentActivity.HID,docotor_hid);
                    ActivityUtils.showActivity(act, CharacteristicDepartmentActivity.class,bundle5);
                    System.out.println("特色科室");
                    break;
                case R.id.tv_service_pro://预约挂号
                    if (hospatalStateBean.getRegistrtype().equals("1")){//1  可点击   2不可点击
                        Bundle bundle7 = new Bundle();
                        bundle7.putString(Constants.FROM,"registered");
                        bundle7.putString(SearchNewDoctorActivity.HID,docotor_hid);
                        bundle7.putBoolean(SearchNewDoctorActivity.ShowRightButtonFlag,true);
                        ActivityUtils.showActivity(act, SearchNewDoctorActivity.class,bundle7);
                    }else{
                        Toast.makeText(act,"挂号未开通",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.tv_service_my_doctor://医生介绍
                    Bundle bundle6 = new Bundle();
                    bundle6.putString(Constants.FROM,"registered");
                    bundle6.putString(SearchNewDoctorActivity.HID,docotor_hid);
                    ActivityUtils.showActivity(act, SearchNewDoctorActivity.class,bundle6);
                    /*Intent intent = new Intent(act, SearchNewDoctorActivity.class);
                    intent.putExtra(Constants.FROM,"registered");
                    act.startActivity(intent);*/
                    //ActivityUtils.showActivity(act, MyHospitalDoctorActivity.class);
                  //;  ActivityUtils.showActivity(act, MyHospitalDoctorActivity.class)
                    System.out.println("我的医生");
                    break;
                case R.id.tv_service_hospital_navigation://院内导航
                    System.out.println("院内导航");
                    Bundle bundle = new Bundle();
                    WebModel webModel = new WebModel();
                    webModel.setShowRightImg1(false);
                    webModel.setShowRightImg2(false);
                    webModel.setTAG_USER_SIGN_RIGHT1(0);
                    webModel.setTAG_USER_SIGN_RIGHT2(0);//显示分享按钮
                    webModel.setTitle("院内导航");
                    webModel.setObject(docotor_hid);
                    bundle.putString(Constants.FROM,"navigation");
                    bundle.putSerializable(WebModel.WEBFLAG_TAG, webModel);
                    ActivityUtils.showActivity(act,NewWebViewActivity.class,bundle);
                    break;
                case R.id.tv_service_survey://满意度调查
                    System.out.println("满意度调查");
                    Bundle bundleSurve = new Bundle();
                    bundleSurve.putString(SatisfactionSurveyQuestionActivity.HID,docotor_hid);
                    ActivityUtils.showActivity(act, SatisfactionSurveyQuestionActivity.class,bundleSurve);
                    break;
            }
        }
    }

    /**
     * 带有头的健康资讯
     */
    class HealthNewsHeaderViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_prefesser_look_more)
        TextView tv_prefesser_look_more;
        @Bind(R.id.iv_hotipal_navigation)
        ImageView iv_hotipal_navigation;
        @Bind(R.id.lv_hotipal_navigation)
        MyListView lv_hotipal_navigation;

        public HealthNewsHeaderViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            //查看更多
            tv_prefesser_look_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            iv_hotipal_navigation.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final MyHomeHospitalConsultationModel myHomeHospitalConsultationModel = hospitalInformationCinlusionModeList.get(0);

                    Bundle bundle = new Bundle();
                    WebModel webModel = new WebModel();
                    //先设置 右侧按钮是否可显示， 默认右侧都不显示，如果只加载URL，则需要 设置webModel.setUrl即可
                    if(myHomeHospitalConsultationModel.getFlag().equals("0")){//不显示收藏按钮
                        webModel.setTAG_USER_SIGN_RIGHT1(0);
                        webModel.setShowRightImg1(false);
                    }else  if(myHomeHospitalConsultationModel.getFlag().equals("1")) {//显示收藏按钮
                        webModel.setTAG_USER_SIGN_RIGHT1(1);
                        webModel.setShowRightImg1(true);
                    }
                    webModel.setShowRightImg2(true);
                    webModel.setTAG_USER_SIGN_RIGHT2(1);//显示分享按钮

                    webModel.setUrl(myHomeHospitalConsultationModel.getTourl());

                    webModel.setId(myHomeHospitalConsultationModel.getId()+"");
                    webModel.setObject(myHomeHospitalConsultationModel.getVtype());
                    bundle.putSerializable(WebModel.WEBFLAG_TAG, webModel);
                    ActivityUtils.showActivity(act,NewWebViewActivity.class, bundle);
                }
            });

        }
    }

    /**
     * 检测是否已注册
     */
    public boolean checkRegister() {
        String username = PreferenceHelper.getString(PreferenceHelper.USERNAME, "");
        if (!TextUtils.isEmpty(username)) {
            //TODO 测试账号只有3位
            boolean isLogin = PreferenceHelper.getBoolean(PreferenceHelper.ISLOGIN, true);
            if (isLogin) {
                return isLogin;
            } else {
                ActivityUtils.showActivity(act, LoginActivity.class);
                return isLogin;
            }
        } else {
            ActivityUtils.showActivity(act, LoginActivity.class);
            return false;
        }
    }

    /**
     * 检测是否已注册
     */
    public boolean isLogin() {
        String username = PreferenceHelper.getString(PreferenceHelper.USERNAME, "");
        if (!TextUtils.isEmpty(username)) {
            //TODO 测试账号只有3位
            boolean isLogin = PreferenceHelper.getBoolean(PreferenceHelper.ISLOGIN, true);
            if (isLogin) {
                return isLogin;
            } else {
                return isLogin;
            }
        } else {
            return false;
        }
    }

    /**
     * 设置progress 用子线程完成动画效果
     *
     * @param view
     * @param target
     */
    private void setProgress(final CircleProgress view, final int target, final int total) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                SystemClock.sleep(500);
                int progress = 0;
                while (progress <= target) {
                    view.setProgress(progress, total);
                    progress++;
                    SystemClock.sleep(10);
                }
            }
        }).start();
    }

    /*public AdsPagerAdapter getAdsPagerAdapter() {
        return adsPagerAdapter;
    }

    public void setAdsPagerAdapter(AdsPagerAdapter adsPagerAdapter) {
        this.adsPagerAdapter = adsPagerAdapter;
    }*/

}
